package com.zwl.test;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Context ctx;

    Handler h = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){


                case  1000:
        showPopwindow();

                    break;
            }
        }
    };
    private TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ctx = this;



        initView();

    }

    private void initView() {


        t = (TextView) findViewById(R.id.text);

        Toast.makeText(ctx,"show  popwindow  .. ",Toast.LENGTH_SHORT).show();
        t.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showPopwindow();
            }
        });
    }

String[] infos = {"全部","待选定","待签约","定向签约","进行中","已汇款","审核中","未通过","已取消"};
    private void showPopwindow() {

        View parent = ((TextView) findViewById(R.id.text));
//        View parent = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        View popView = View.inflate(ctx, R.layout.pop_menu, null);

        Button btnCamera = (Button) popView.findViewById(R.id.btn_login_again);
        Button btnAlbum = (Button) popView.findViewById(R.id.btn_exit);
        Button btnCancel = (Button) popView.findViewById(R.id.btn_pop_cancel);

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(popView, width, height);
        popWindow.setAnimationStyle(R.style.AnimBottom1);
        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失

        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_login_again:
                        // reLogin app

                        startActivity(new Intent(MainActivity.this,DialogActivity.class));
                        break;
                    case R.id.btn_exit:
                        // exit app
                        //设置状态为未登录
                        // PreferenceUtil.setUserIsLogin(ctx, false);
                        startActivity(new Intent(MainActivity.this,DialogActivity2.class));
                        break;
                    case R.id.btn_pop_cancel:
                        // do nothing.
                        break;
                }
                popWindow.dismiss();
            }
        };

        btnCamera.setOnClickListener(listener);
        btnAlbum.setOnClickListener(listener);
        btnCancel.setOnClickListener(listener);

        ColorDrawable dw = new ColorDrawable(0x30000000);
        popWindow.setBackgroundDrawable(dw);
//        popWindow.showAtLocation(parent, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
        popWindow.showAsDropDown(parent,0,0);
    }

}
